
pub mod models;
pub mod paths;
pub mod file_ops;
pub mod json_ops;
pub mod image_ops;
pub mod fs_tool{
    use super::*;

    use std::fs;
    use std::path::Path;
    use serde_json::{Value, json};
    use models::{DataResponse, ImageData};
    use paths::creat_path;

pub fn exist_init_file(base_path: &str) {
    let file_path = creat_path(base_path);
    file_ops::add_file(file_path._user_path.as_str(), Some("user"));
    file_ops::add_file(file_path._other_path.as_str(), None);
    file_ops::add_file(file_path._log_path.as_str(), None);
    file_ops::add_dir(file_path._dir_image_path.as_str(), None);
    file_ops::add_dir(file_path._dir_file_path.as_str(), None);
    file_ops::add_dir(&file_path._dir_image_user_path, Some("user"));
}

pub fn get_all_data(base_path: &str) -> Result<DataResponse, String> {
    let base = Path::new(base_path);
    let file_path = creat_path(base_path);
    
    let log_content = fs::read_to_string(&file_path._log_path)
        .map_err(|e| format!("日志读取失败: {}", e))?;
    
    let log_json: Value = serde_json::from_str(&log_content)
        .map_err(|e| format!("日志解析失败: {} (行{}列{})", e, e.line(), e.column()))?;

    let user_content = fs::read_to_string(&file_path._user_path)
        .map_err(|e| format!("用户文件读取失败: {}", e))?;
    
    let user_json: Value = serde_json::from_str(&user_content)
        .map_err(|e| format!("用户数据解析失败: {} (行{}列{})", e, e.line(), e.column()))?;

    let images = image_ops::read_dir_all_image(&file_path._dir_image_path);
    let user_images = image_ops::read_dir_all_image(&file_path._dir_image_user_path);
  
    Ok(DataResponse {
        log: log_json,
        images,
        user: user_json,
        user_images,
    })
}

pub fn add_log(_path: &str, _json: &str) {
    let file_path = creat_path(_path);
    let entry: Value = serde_json::from_str(_json).unwrap();
    
    if entry["mes_type"].as_str() == Some("image") {
        image_ops::add_image(&file_path._dir_image_path, &entry);
    }
    
    json_ops::add_file_json(&file_path._log_path, _json).unwrap();
}

pub fn add_user(_path: &str, _json: &str) {
    let file_path = creat_path(_path);
    let entry: Value = serde_json::from_str(_json).unwrap();
    image_ops::add_image(&file_path._dir_image_user_path, &entry);
    json_ops::add_file_json(&file_path._user_path, _json).unwrap();
}

pub fn delete_log(_path: &str, _json: &str) {
    let file_path = creat_path(_path);
    let entry: Value = serde_json::from_str(_json).unwrap();
    
    if entry["mes_type"].as_str() == Some("image") {
        image_ops::delete_image(&file_path._dir_image_path, &entry);
    }
    
    json_ops::delete_file_json(Path::new(&file_path._log_path), _json).unwrap();
}

pub fn delete_user(_path: &str, _json: &str) {
    let file_path = creat_path(_path);
    let entry: Value = serde_json::from_str(_json).unwrap();
    json_ops::delete_user_mes(Path::new(&file_path._log_path), _json,_path).unwrap();
    image_ops::delete_image(&file_path._dir_image_user_path, &entry);
    json_ops::delete_file_json(Path::new(&file_path._user_path), _json).unwrap();
}

pub fn edit_user(_path: &str, _json: &str) {
    let file_path = creat_path(_path);
    let entry: Value = serde_json::from_str(_json).unwrap();
    image_ops::add_image(&file_path._dir_image_user_path, &entry);
    json_ops::edit_file_json(&file_path._user_path, _json).unwrap();
}}